import pygame,sys,os


class MusicManager:
    _instance = None

    def __new__(cls):
        if not cls._instance:
            cls._instance = super().__new__(cls)
            cls._instance.init_music()
        return cls._instance

    def init_music(self):
        pygame.mixer.init()
        self.muted = False
        pygame.mixer.music.load(self.resource_path("source/sound/back.mp3"))  # 背景音乐文件
        pygame.mixer.music.set_volume(0.5)
        pygame.mixer.music.play(-1)  # 循环播放

    def toggle_mute(self):
        self.muted = not self.muted
        pygame.mixer.music.set_volume(0 if self.muted else 0.5)
    def resource_path(self,relative_path):
        """ 根据打包后的环境动态加载资源路径 """
        if hasattr(sys, '_MEIPASS'):
            base_path = sys._MEIPASS
        else:
            base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)
